<?php

class BillController extends BackendApplicationController {

	public $layout = 'application.modules.bill.views.layouts.global';
	public function init()
	{
		Yii::app()->getClientScript()->registerCoreScript( 'jquery.ui' );
		Yii::app()->clientScript->registerCssFile(
		Yii::app()->clientScript->getCoreScriptUrl().
		'/jui/css/base/jquery-ui.css'
		);
		parent::init();
	}
	public function actionView() {
		$id = Yii::app()->request->getParam('id', 0);
		if(isset($_POST['SearchBill'])){
			$amBill = $_POST['SearchBill']['bill_code'];
			$id = Yii::app()->db->createCommand("select id from bill where am_bill='$amBill'")->queryScalar();
			if(!$id){
				Yii::app()->user->setState('msg',"Không tìm thấy Bill nào có mã là: ".$amBill);
			}else{
				Yii::app()->user->setState('msg',"");
			}
		}
		if($id>0){
			$model = $this->loadModel($id, 'BackendBillModel');
		}else{
			$model = null;
		}
		$this->render('view', array(
			'model' => $model,
		));
	}

	public function actionCreate() {
		$model = new BackendBillModel;
		if (isset($_POST['BackendBillModel'])) {
			$dateTime = Common::ConvertDatetime($_POST['BackendBillModel']['created_datetime']);
			$model->setAttributes($_POST['BackendBillModel']);
			$model->setAttribute('created_datetime', $dateTime);
			if ($model->save()) {
				if (Yii::app()->getRequest()->getIsAjaxRequest())
					Yii::app()->end();
				else
					$this->redirect(array('view', 'id' => $model->id));
			}
		}

		$this->render('create', array( 'model' => $model));
	}

	public function actionUpdate($id) {
		$model = $this->loadModel($id, 'BackendBillModel');

		if (isset($_POST['BackendBillModel'])) {
			$dateTime = Common::ConvertDatetime($_POST['BackendBillModel']['created_datetime']);
			$model->setAttributes($_POST['BackendBillModel']);
			$model->setAttribute('created_datetime', $dateTime);
			if ($model->save()) {
				$this->redirect(array('view', 'id' => $model->id));
			}
		}

		$this->render('update', array(
				'model' => $model,
				));
	}

	public function actionDelete($id) {
		if (Yii::app()->getRequest()->getIsPostRequest()) {
			$this->loadModel($id, 'BackendBillModel')->delete();

			if (!Yii::app()->getRequest()->getIsAjaxRequest())
				$this->redirect(array('admin'));
		} else
			throw new CHttpException(400, Yii::t('app', 'Your request is invalid.'));
	}

	public function actionIndex() {
		$dataProvider = new CActiveDataProvider('BackendBillModel');
		$this->render('index', array(
			'dataProvider' => $dataProvider,
		));
	}

	public function actionAdmin() {
		$model = new BackendBillModel('search');
		$model->unsetAttributes();

		if (isset($_GET['BackendBillModel']))
			$model->setAttributes($_GET['BackendBillModel']);

		$this->render('admin', array(
			'model' => $model,
		));
	}
	public function actionGetTypeCpn()
	{
		$type = Yii::app()->request->getParam('type',0);
		$sql = "SELECT * FROM bill_type_cpn WHERE type=$type";
		$data = Yii::app()->db->createCommand($sql)->queryAll();
		$this->layout=false;
		$html = "";
		foreach ($data as $value){
			$html.="<option value='{$value['code']}'>{$value['name']}</option>";
		}
		echo $html;
	}
}